﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRMS.Models;
using DataManager;
using HRMS_Model;
using Lib.Web.Mvc.JQuery.JqGrid;
using System.Globalization;
using HRMS.Models.ModelBinders;
namespace HRMS.Controllers
{
    public class ClientController : Controller
    {
        //
        // GET: /Client/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Clients(JqGridRequest request)
        {
            //get the count of roles
            var totalRecords = ClientsModels.GetClientsCount();

            //Prepare JqGridData instance
            var response = new JqGridResponse
            {
                //Total pages count
                TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
                //Page number
                PageIndex = request.PageIndex,
                //Total records count
                TotalRecordsCount = totalRecords
            };

            //Get Designations
            var clients = ClientsModels.GetClients(request.SortingName.ToString(CultureInfo.InvariantCulture), request.SortingOrder.ToString(), request.PageIndex, request.RecordsCount, request.PagesCount.HasValue ? request.PagesCount.Value : 1);

            //Table with rows data
            foreach (var client in clients)
            {
                response.Records.Add(new JqGridRecord(Convert.ToString(client.ClientId), new List<object>() { 
                                    client.ClientId,
                client.CompanyName,
                client.Address,
                client.City,
                client.State,
                client.Zip,
                client.ContactName,
                client.Designation,
                client.Telephone,
                client.Fax,
                client.Email
                }));
            }

            //Return data as json
            return new JqGridJsonResult() { Data = response };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertClient(Client clientModel)
        {
            return Json(ClientsModels.AddClient(clientModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateClient([ModelBinder(typeof(ClientModelBinder))]Client clientModel)
        {
            return Json(ClientsModels.UpdateClient(clientModel));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteClient(int id)
        {
            return Json(ClientsModels.DeleteClient(id));
        }
    }
}